<template>
    <div id="specialtyline">
        <div class="title">专业录取分数线</div>

        <el-card class="main">
            <!-- 标题 -->
            <div class="main_header">
                <div class="main_header_title">院校专业列表</div>
                <div class="main_header_tool">
                    <el-button class="tool_btn" type="primary">批量导入</el-button>
                    <el-button class="tool_btn" type="primary" @click="addInfo(getAlldata)">新增</el-button>
                </div>
            </div>

            <!-- 查询 -->
            <div class="main_query">
                <div class="main_query_form">
                    <el-form 
                        :inline="true" 
                        :model="queryForm" 
                        class="demo-form-inline"
                        ref="queryRuleFormRef"
                        :rules="queryRule"
                        :show-message="false"
                    >
                        <el-row :gutter="18">
                            <el-col :span="6">
                                <el-form-item label="学校：" prop="school">
                                    <el-input v-model="queryForm.school" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="高考省份/直辖市：" prop="address">
                                    <el-input v-model="queryForm.address" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="年份：" prop="year">
                                    <el-input v-model="queryForm.year" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="18">
                            <el-col :span="6">
                                <el-form-item label="专业：" prop="specialty">
                                    <el-input v-model="queryForm.specialty" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="报考科目类别：" prop="specialtyCategory">
                                    <el-input v-model="queryForm.specialtyCategory" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="6">
                                <el-form-item label="录取批次：" prop="batch">
                                    <el-input v-model="queryForm.batch" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
                <div class="main_query_tool">
                    <el-button class="tool_btn" type="primary" @click="onQuerySubmit(queryRuleFormRef, getAlldata)">查询</el-button>
                </div>
            </div>

            <!-- 数据显示 -->
            <el-table
                ref="multipleTableRef"
                :data="tableData"
                style="width: 100%;height: 520px;"
                @selection-change="handleSelectionChange"
                class="main_table"
            >
                <el-table-column type="selection" />
                <el-table-column property="school" label="学校" />
                <el-table-column property="province" label="高考省份/直辖市" />
                <el-table-column property="year" label="年份"  />
                <el-table-column property="showRequirements" label="报考科目要求" />
                <el-table-column property="professional" label="专业" />
                <el-table-column property="batch" label="批次" />
                <el-table-column property="code" label="所属专业组" />
                <el-table-column property="score" label="录取分数线" />
                <el-table-column label="详细信息" >
                    <template #default="scope">
                        <el-button class="tool_btn check" type="primary" @click="checkInfo(scope.row)">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200px">
                    <template #default="scope">
                        <el-button class="tool_btn edit" type="primary" @click="handleredit(scope.row)">编辑</el-button>
                        <el-popconfirm 
                            title="是否删除?"
                            confirm-button-text="是"
                            cancel-button-text="否"
                            :icon="InfoFilled"
                            icon-color="#626AEF"
                            @confirm="handleDel(getAlldata,scope.row)"
                        >
                        <template #reference>
                            <el-button class="tool_btn del" type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                        
                    </template>
                </el-table-column>
            </el-table>

            <!-- 批量删除 -->
            <div class="allDel">
                <el-button text @click="delBtn(getAlldata)">批量删除</el-button >
            </div>

            <!-- 分页 -->
            <el-pagination 
                background 
                layout="prev, pager, next" 
                :total="total"
                v-model:page-size="params.pagesize"
                v-model:current-page="params.pagenum"
                @current-change="changePage"
            />
        </el-card>

        <!-- 查看对话框 -->
        <el-dialog v-model="dialogFormVisible" title="查看详细信息">
            <el-form :model="checkInfoData" class="checkInfo">
                <!-- <el-row :gutter="20">
                    <el-col :span="10"> -->
                        <el-form-item label="学校名称:" :label-width="formLabelWidth">
                            <p>{{checkInfoData.school}}</p>
                        </el-form-item>
                    <!-- </el-col> -->
                    <!-- <el-col :span="10"> -->
                        <el-form-item label="年份:" :label-width="formLabelWidth">
                            <p>{{checkInfoData.year}}</p>
                        </el-form-item>
                    <!-- </el-col> -->
                <!-- </el-row> -->
                <el-form-item label="录取专业:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.professional}}</p>
                </el-form-item>
                <el-form-item label="所属专业组代码:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.code}}</p>
                </el-form-item>
                <el-form-item label="报考科目要求:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.requirements}}</p>
                </el-form-item>
                <el-form-item label="招生计划:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.plan}}</p>
                </el-form-item>
                <el-form-item label="学制:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.educationalSystem}}</p>
                </el-form-item>
            </el-form>
            <el-form :model="checkInfoData" class="checkInfo">
                <el-form-item label="高考省份/直辖市:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.province}}</p>
                </el-form-item>
                <el-form-item label="录取批次:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.batch}}</p>
                </el-form-item>   
                <el-form-item label="专业代码:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.belongs}}</p>
                </el-form-item>
                <el-form-item label="最低投档分数:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.score}}</p>
                </el-form-item>
                <el-form-item label="录取人数:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.enrollment}}</p>
                </el-form-item>
                <el-form-item label="学费:" :label-width="formLabelWidth">
                    <p>{{checkInfoData.cost}}</p>
                </el-form-item>
            </el-form>
            <template #footer>
            <span class="dialog-footer"></span>
            </template>
        </el-dialog>

        <!-- 修改对话框 -->
        <el-dialog title="专业分数线信息编辑" v-model="editFormVisible" width="50%" center @close="editResetForm(editRuleFormRef)">
            <el-form :inline="true" :model="editform" :rules="editFormRules" ref="editRuleFormRef" :hide-required-asterisk="true">
                <el-form-item label="学校名称：" label-width="140px" prop="school">
                    <el-input v-model.trim="editform.school" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="高考省份/直辖市：" label-width="140px" prop="province">
                    <el-input v-model.trim="editform.province" style="width: 120px;"  />
                </el-form-item>
                <el-form-item label="年份：" label-width="140px" prop="year">
                    <el-input v-model.number="editform.year" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="录取批次：" label-width="140px" prop="batch">
                    <!-- <el-input v-model.trim="editform.batch" placeholder="-" class="batch" style="width: 120px;text-align: center;margin-right: 10px;"  /> -->
                    <el-select v-model.trim="editform.batch" placeholder="-" class="batch" style="width: 120px;text-algin: center;margin-right: 10px;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="本科" value="本科" />
                        <el-option label="专科" value="专科" />
                    </el-select>
                </el-form-item>
                <el-form-item label="录取专业：" label-width="140px" prop="professional">
                    <el-input v-model.number="editform.professional" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="专业代码：" label-width="140px" prop="belongs">
                    <el-input v-model.number="editform.belongs" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="所属专业组代码：" label-width="140px" prop="code">
                    <!-- <el-input v-model.number="editform.code" style="width: 120px;" /> -->
                    <el-select v-model.trim="editform.code" placeholder="-" class="code" style="width: 120px;text-algin: center;margin-right: 10px;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="01" value="01" />
                        <el-option label="02" value="02" />
                        <el-option label="03" value="03" />
                    </el-select>
                </el-form-item>
                <el-form-item label="最低投档分数：" label-width="140px" prop="score">
                    <el-input v-model.number="editform.score" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="报考科目要求：" label-width="140px" prop="requirements">
                    <el-select v-model.trim="editform.requirements" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="140px" prop="requirements1">
                    <el-select v-model.trim="editform.requirements1" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="140px" prop="requirements2">
                    <el-select v-model.trim="editform.requirements2" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label="招生计划：" label-width="140px" prop="plan">
                    <el-input v-model.number="editform.plan" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="录取人数：" label-width="140px" prop="enrollment">
                    <el-input v-model.number="editform.enrollment" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="学制：" label-width="140px" prop="educationalSystem">
                    <el-input v-model.trim="editform.educationalSystem" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="学费：" label-width="140px" prop="cost">
                    <el-input v-model="editform.cost" style="width: 120px;" />
                </el-form-item>
            </el-form>
            <template #footer>
            <span class="dialog-footer">
                <el-button type="error"  @click="editResetForm(editRuleFormRef)">取消</el-button>
                <el-button type="success"  @click="editSubmitForm(editRuleFormRef,getAlldata)">提交</el-button>
            </span>
            </template>
        </el-dialog>

         <!-- 新增对话框 -->
        <el-dialog title="新增专业分数线信息" v-model="addFormVisible" width="50%" center @close="closeDialog(addRuleFormRef)">
            <el-form :inline="true" :model="addform" :rules="addFormRules" ref="addRuleFormRef" :hide-required-asterisk="true">
                <el-form-item label="学校名称：" label-width="140px" prop="school">
                    <el-input v-model.trim="addform.school" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="高考省份/直辖市：" label-width="140px" prop="province">
                    <el-input v-model.trim="addform.province" style="width: 120px;"  />
                </el-form-item>
                <el-form-item label="年份：" label-width="140px" prop="year">
                    <el-input v-model.number="addform.year" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="录取批次：" label-width="140px" prop="batch">
                    <!-- <el-input v-model.trim="addform.batch" placeholder="-" class="batch" style="width: 120px;text-align: center;margin-right: 10px;"  /> -->
                    <el-select v-model.trim="addform.batch" placeholder="-" class="batch" style="width: 120px;text-algin: center;margin-right: 10px;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="本科" value="本科" />
                        <el-option label="专科" value="专科" />
                    </el-select>
                </el-form-item>
                <el-form-item label="录取专业：" label-width="140px" prop="professional">
                    <el-input v-model.trim="addform.professional" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="专业代码：" label-width="140px" prop="belongs">
                    <el-input v-model.number="addform.belongs" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="所属专业组代码：" label-width="140px" prop="code">
                    <!-- <el-input v-model.number="addform.code" style="width: 120px;" /> -->
                    <el-select v-model.trim="addform.code" placeholder="-" class="code" style="width: 120px;text-algin: center;margin-right: 10px;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="01" value="01" />
                        <el-option label="02" value="02" />
                        <el-option label="03" value="03" />
                    </el-select>
                </el-form-item>
                <el-form-item label="最低投档分数：" label-width="140px" prop="score">
                    <el-input v-model.number="addform.score" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="报考科目要求：" label-width="140px" prop="requirements">
                    <!-- <el-input v-model.trim="editform.requirements" placeholder="-" class="batch" style="width: 120px;"  /> -->
                    <el-select v-model.trim="addform.requirements" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="140px" prop="requirements1">
                    <!-- <el-input v-model="addform.requirements1" placeholder="-" class="batch" style="width: 120px;"  /> -->
                    <el-select v-model.trim="addform.requirements1" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label-width="140px" prop="requirements2">
                    <!-- <el-input v-model="addform.requirements2" placeholder="-" class="batch" style="width: 120px;"  /> -->
                    <el-select v-model.trim="addform.requirements2" placeholder="-" class="batch" style="width: 120px;text-algin: center;" suffix-icon="">
                        <el-option label="-" value="-" />
                        <el-option label="语文" value="语文" />
                        <el-option label="数学" value="数学" />
                        <el-option label="外语" value="外语" />
                        <el-option label="政治" value="政治" />
                        <el-option label="历史" value="历史" />
                        <el-option label="地理" value="地理" />
                        <el-option label="物理" value="物理" />
                        <el-option label="化学" value="化学" />
                        <el-option label="生物" value="生物" />
                    </el-select>
                </el-form-item>
                <el-form-item label="招生计划：" label-width="140px" prop="plan">
                    <el-input v-model.number="addform.plan" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="录取人数：" label-width="140px" prop="enrollment">
                    <el-input v-model.number="addform.enrollment" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="学制：" label-width="140px" prop="educationalSystem">
                    <el-input v-model.trim="addform.educationalSystem" style="width: 120px;" />
                </el-form-item>
                <el-form-item label="学费：" label-width="140px" prop="cost">
                    <el-input v-model="addform.cost" style="width: 120px;" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="info"  @click="addResetForm(addRuleFormRef)">取消</el-button>
                    <el-button type="success"  @click="addSubmitForm(addRuleFormRef,getAlldata)">提交</el-button>
                </span>
            </template>
        </el-dialog>

    </div>
</template>

<script>
import { reactive, toRefs, ref } from 'vue'
import { admissionList, addAdmissionList, editAdmissionList, queryAdmissionList, delAdmissionList }  from '@/api/axiosPlus.js'
import { InfoFilled } from '@element-plus/icons-vue'

// 获取数据
function useGetData () {
    let data = reactive({
        tableData:[],
        total: 0,
        params: {
            pagenum: 1,
            pagesize: 5
        }
    })
    const getAlldata = async (queryData=[]) => {
        const res = await admissionList({
            params: data.params
        })
        console.log(res)
        
        if(queryData.length === 0) {
            data.tableData = res.result
            // 处理科目渲染
            data.tableData = data.tableData.filter(v => {
                return v.showRequirements = v.requirements+'/'+v.requirements1+'/'+v.requirements2
            })
            console.log(data.tableData);
            data.total = res.total
        } else {
            data.tableData = queryData
            // 处理科目渲染
            data.tableData = data.tableData.filter(v => {
                return v.showRequirements = v.requirements+'/'+v.requirements1+'/'+v.requirements2
            })
        }
    }

    // 分页
    const changePage = (val) => {
        data.params.pagenum = val
        getAlldata()
    }

    return {
        ...toRefs(data),
        getAlldata,
        changePage
    }
}

// 查看
function useCheckInfo() {
    // const dialogTableVisible = ref(false)
    const dialogFormVisible = ref(false)
    const formLabelWidth = '140px'
    let checkScoreInfo = reactive({checkInfoData: {}}) 
    const checkInfo = (row) => {
        dialogFormVisible.value = true
        checkScoreInfo.checkInfoData = row
        console.log(checkScoreInfo);
    }

    return {
        // dialogTableVisible,
        dialogFormVisible,
        formLabelWidth,
        checkInfo,
       ...toRefs(checkScoreInfo),
        
    }
}

// 编辑数据
function useEditData () {
    // 编辑对话框  对话框显示状态 编辑表单
    const schoolPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学校名称'))
        } else {
            callback()
        }
    }
    const provincePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入省份/直辖市'))
        } else {
            callback()
        }
    }
    const yearPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入年份'))
        } else {
            callback()
        }
    }
    const requirementsPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == editformData.editform.requirements1 || value == editformData.editform.requirements2) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const requirements1Pass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == editformData.editform.requirements || value == editformData.editform.requirements2) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const requirements2Pass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == editformData.editform.requirements || value == editformData.editform.requirements1) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const batchPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请选择录取批次'))
        } else {
            callback()
        }
    }
    const professionalPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取专业'))
        } else {
            callback()
        }
    }
    const belongsPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入专业代码）'))
        } else {
            callback()
        }
    }
    const scorePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取分数'))
        } else {
            callback()
        }
    }
    const planPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入招生计划人数'))
        } else {
            callback()
        }
    }
    const enrollmentPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取人数'))
        } else {
            callback()
        }
    }
    const codePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请选择所属专业组代码'))
        } else {
            callback()
        }
    }
    const educationalSystemPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学制'))
        } else {
            callback()
        }
    }
    const costPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学费'))
        } else {
            callback()
        }
    }
    const editRuleFormRef = ref()
    const editFormVisible = ref(false)
    const editformData = reactive({
        editform: {
            bt_id: '',
            school:'',
            province: "",
            year: '',
            requirements:'',
            requirements1:'',
            requirements2:'',
            professional:'',
            batch: '',
            belongs: "",
            score: '',
            plan: '',
            enrollment: '',
            code:'',
            educationalSystem:'',
            cost:''
        },
        isSubmitform: {
            bt_id: '',
            school:'',
            province: "",
            year: '',
            requirements:'',
            requirements1:'',
            requirements2:'',
            professional:'',
            batch: '',
            belongs: "",
            score: '',
            plan: '',
            enrollment: '',
            code:'',
            educationalSystem:'',
            cost:''
        },
        editFormRules: {
            school: [{ validator: schoolPass, trigger: 'blur' }],
            province: [{ validator: provincePass, trigger: 'blur' }],
            year: [{ validator: yearPass, trigger: 'blur' }],
            requirements: [{ validator: requirementsPass, trigger: 'change' }],
            requirements1: [{ validator: requirements1Pass, trigger: 'change' }],
            requirements2: [{ validator: requirements2Pass, trigger: 'change' }],
            professional: [{ validator: professionalPass, trigger: 'blur' }],
            batch: [{ validator: batchPass, trigger: 'change' }],
            belongs: [{ validator: belongsPass, trigger: 'blur' }],
            score: [{ validator: scorePass, trigger: 'blur' }],
            plan: [{ validator: planPass, trigger: 'blur' }],
            enrollment: [{ validator: enrollmentPass, trigger: 'blur' }],
            code: [{ validator: codePass, trigger: 'change' }],
            educationalSystem: [{ validator: educationalSystemPass, trigger: 'blur' }],
            cost: [{ validator: costPass, trigger: 'blur' }],
        },
    })

    // 编辑
    const handleredit = (item) => {
        editFormVisible.value = true
        editformData.editform = JSON.parse(JSON.stringify(item))
        editformData.isSubmitform = JSON.parse(JSON.stringify(item))
        console.log(item)
    }

    // 提交按钮
    const editSubmitForm = (editRuleFormRef,callback) => {
        if (!editRuleFormRef) return
        editRuleFormRef.validate(async (valid) => {
            if (valid) {
                // console.log(JSON.stringify(editformData.editform) == JSON.stringify(editformData.isSubmitform))
                if (JSON.stringify(editformData.editform) == JSON.stringify(editformData.isSubmitform)) {
                    ElMessage({
                        message: '请修改信息！',
                        type: 'warning',
                        duration: 2000
                    })
                } else {
                    const res = await editAdmissionList({
                        id: editformData.editform.bt_id,
                        school:editformData.editform.school,
                        province: editformData.editform.province,
                        year: editformData.editform.year,
                        requirements: editformData.editform.requirements,
                        requirements1: editformData.editform.requirements1,//报考科目要求
                        requirements2: editformData.editform.requirements2,//报考科目要求
                        professional: editformData.editform.professional,
                        batch: editformData.editform.batch,
                        belongs: editformData.editform.belongs,
                        score: editformData.editform.score,
                        plan: editformData.editform.plan,
                        enrollment: editformData.editform.enrollment,
                        code: editformData.editform.code,
                        educationalSystem: editformData.editform.educationalSystem,
                        cost: editformData.editform.cost
                    })
                    console.log(res);
                    editFormVisible.value = false
                    callback()
                }
                
            } else {
                console.log('error submit!')
                editFormVisible.value = false
                return false
            }
        })
    }

    // 取消提交
    const editResetForm = (formEl) => {
        if (!formEl) return
        formEl.resetFields()
        editFormVisible.value = false
    }

    return {
        editFormVisible,
        editRuleFormRef,
        ...toRefs(editformData),
        handleredit,
        editSubmitForm,
        editResetForm
    }
}

// 新增
function useAddInfo () {
    const addFormVisible = ref(false)
    const addRuleFormRef = ref([])
    const addformData = reactive({
        addform: {
            school:'',
            province: "",
            year: '',
            requirements:'',
            requirements1:'',
            requirements2:'',
            professional:'',
            batch: '',
            belongs: "",
            score: '',
            plan: '',
            enrollment: '',
            code:'',
            educationalSystem:'',
            cost:''
        }
    })

    const schoolPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学校名称'))
        } else {
            callback()
        }
    }
    const provincePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入省份/直辖市'))
        } else {
            callback()
        }
    }
    const yearPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入年份'))
        } else {
            callback()
        }
    }
    const requirementsPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == addformData.addform.requirements1 || value == addformData.addform.requirements2) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const requirements1Pass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == addformData.addform.requirements || value == addformData.addform.requirements2) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const requirements2Pass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入报考科目要求'))
        } else if (value == '-') {
            callback()
        } else if (value == addformData.addform.requirements || value == addformData.addform.requirements1) {
            callback(new Error('请输入不同的报考科目要求'))
        } else {
            callback()
        }
    }
    const batchPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请选择录取批次'))
        } else {
            callback()
        }
    }
    const professionalPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取专业'))
        } else {
            callback()
        }
    }
    const belongsPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入专业代码）'))
        } else {
            callback()
        }
    }
    const scorePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取分数'))
        } else {
            callback()
        }
    }
    const planPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入招生计划人数'))
        } else {
            callback()
        }
    }
    const enrollmentPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入录取人数'))
        } else {
            callback()
        }
    }
    const codePass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请选择所属专业组代码'))
        } else {
            callback()
        }
    }
    const educationalSystemPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学制'))
        } else {
            callback()
        }
    }
    const costPass = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请输入学费'))
        } else {
            callback()
        }
    }
    const addFormRules = ({
        school: [{ validator: schoolPass, trigger: 'blur' }],
        province: [{ validator: provincePass, trigger: 'blur' }],
        year: [{ validator: yearPass, trigger: 'blur' }],
        requirements: [{ validator: requirementsPass, trigger: 'change' }],
        requirements1: [{ validator: requirements1Pass, trigger: 'change' }],
        requirements2: [{ validator: requirements2Pass, trigger: 'change' }],
        professional: [{ validator: professionalPass, trigger: 'blur' }],
        batch: [{ validator: batchPass, trigger: 'change' }],
        belongs: [{ validator: belongsPass, trigger: 'blur' }],
        score: [{ validator: scorePass, trigger: 'blur' }],
        plan: [{ validator: planPass, trigger: 'blur' }],
        enrollment: [{ validator: enrollmentPass, trigger: 'blur' }],
        code: [{ validator: codePass, trigger: 'change' }],
        educationalSystem: [{ validator: educationalSystemPass, trigger: 'blur' }],
        cost: [{ validator: costPass, trigger: 'blur' }],
    })

    // 添加按钮
    const addInfo = () => {
        addFormVisible.value = true
    }

    // 提交
    const addSubmitForm = (formEl, callback) => {
        if(!formEl) return
        formEl.validate(async (valid,val) => {
            if (valid) {
                console.log(addformData.addform)
                const res = await addAdmissionList({
                    school: addformData.addform.school, //学校
                    province: addformData.addform.province, //省份
                    year: addformData.addform.year,//年份
                    requirements: addformData.addform.requirements,//报考科目要求
                    requirements1: addformData.addform.requirements1,//报考科目要求
                    requirements2: addformData.addform.requirements2,//报考科目要求
                    code: addformData.addform.code,//专业组代码
                    professional: addformData.addform.professional,
                    batch: addformData.addform.batch,
                    score: addformData.addform.score,//最低投档分
                    belongs: addformData.addform.belongs,
                    plan: addformData.addform.plan,//招生计划
                    enrollment: addformData.addform.enrollment,
                    educationalSystem: addformData.addform.educationalSystem,
                    cost: addformData.addform.cost,
                })
                console.log(res);
                formEl.resetFields()
                addFormVisible.value = false
                ElMessage({
                    message: '添加成功！',
                    type: 'success',
                    duration: 2000
                })
                callback(true)
            } else {
                console.log('error submit!',val)
                // addFormVisible.value = false
                return false
            }
        })

    }
    // 取消按钮
    const addResetForm = (formEl) => {
        if(!formEl) return
        addFormVisible.value = false
        formEl.resetFields()
    }
    // 关闭对话框
    const closeDialog = (formEl) => {
        if (!formEl) return
        addFormVisible.value = false
        formEl.resetFields()
    }


    return {
        addFormVisible,
        addInfo,
        addResetForm,
        closeDialog,
        addRuleFormRef,
        addFormRules,
        addSubmitForm,
        ...toRefs(addformData)
    }
}

// 查询
function useQuery() {
    const queryList = reactive({
        queryForm:{
            school:'',
            address: '',
            year: '',
            specialty: '',
            specialtyCategory:'',
            batch: ''
        }
        
    })
    const queryRuleFormRef = ref()
    const schoolPass = (rule, value, callback) => {
        if (value === '') { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    }

    const addressPass = (rule, value, callback) => {
        if (value === '') { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    }
    const yearPass = (rule, value, callback) => {
        if (value === '') { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    }
    const specialtyPass = (rule, value, callback) => {
        if (!value) { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    }
    const specialtyCategoryPass = (rule, value, callback) => {
        if (!value) { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    } 
    const batchPass = (rule, value, callback) => {
        if (!value) { 
            callback(new Error('Please input the password again'))
        }else {
            callback()
        }
    }
    const queryRule = reactive({
        school:[{validator:schoolPass, trigger: 'change'}],
        address:[{validator:addressPass, trigger: 'change'}],
        year:[{validator:yearPass, trigger: 'change'}],
        specialty:[{validator:specialtyPass, trigger: 'change'}],
        specialtyCategory:[{validator:specialtyCategoryPass, trigger: 'change'}],
        batch:[{validator:batchPass, trigger: 'change'}],
    })

    // 查询按钮
    const onQuerySubmit = (queryRuleFormRef,callback) => {
        if (!queryRuleFormRef) return
        queryRuleFormRef.validate( async (valid, val) => {
            if (valid) {
                console.log('submit!')
                // 校验成功就发送查询请求
                const res = await queryAdmissionList({
                    school: queryList.queryForm.school,
                    province:queryList.queryForm.province,
                    year:queryList.queryForm.year,
                    requirements:queryList.queryForm.requirements,
                    professional:queryList.queryForm.professional,
                    batch:queryList.queryForm.batch,
                })
                queryRuleFormRef.resetFields() // 表单重置
                // 没有数据的情况
                if(res.data.length === 0) ElMessage({
                    message: '暂无查询信息！',
                    type: 'warning',
                    duration: 2000
                })
                callback(res.data)
            } else {
                console.log('error submit!',val)
                if(Object.getOwnPropertyNames(val).length === 6) {
                    callback()
                    setTimeout(() => {
                        queryRuleFormRef.resetFields()
                    },2000)
                    ElMessage({
                        message: '至少要输入一个查询条件！',
                        type: 'warning',
                        duration: 3000
                    })
                } else {
                // 当用户填写任意一个查询条件时 发送查询请求
                    const res = await queryAdmissionList({
                        school: queryList.queryForm.school,
                        province:queryList.queryForm.province,
                        year:queryList.queryForm.year,
                        requirements:queryList.queryForm.requirements,
                        professional:queryList.queryForm.professional,
                        batch:queryList.queryForm.batch,
                    })

                    console.log(res);
                    if(res.data.length === 0) {
                        ElMessage({
                        message: '暂无查询信息！',
                        type: 'warning',
                        duration: 2000
                        })
                    } else {
                        ElMessage({
                        message: '查询成功',
                        type: 'success',
                        duration: 2000
                        })
                        queryRuleFormRef.resetFields()
                    }
                    callback(res.data)
                } 
                return false
            }
        })
    }

    return {
        ...toRefs(queryList),
        queryRuleFormRef,
        queryRule,
        onQuerySubmit
    }
}


// 删除
function useDelete () {
    const handleDel = async (callback , row) => {
        const id = row.bt_id+''
        console.log(id);
        const res = await delAdmissionList({
            params:{
                id
            }
        })
        console.log(res);
        callback()
        ElMessage({
            message: '删除成功！',
            type: 'success',
            duration: 2000
        })
    }
    // 批量删除
    let tableSelection = []
    const handleSelectionChange = (val) => {
        console.log(val)
    // 找到sc_id并存在数组里面
    tableSelection = val.map(v => v.bt_id)
    console.log(tableSelection);
    }

    const delBtn = async (callback) => {
    console.log('批量删除');
    // 用逗号拼接
    const idArr = tableSelection.join(',')
    if (!idArr)  return ElMessage({
      message: '请选择需要删除的数据！',
      type: 'warning',
    })

    const res = await delAdmissionList({
      params: {
        id: idArr
      }
    })
    console.log(res);
    callback()
    ElMessage({
            message: '删除成功！',
            type: 'success',
            duration: 2000
          })
    }
    return {
        handleDel,
        handleSelectionChange,
        delBtn
    }
}
export default {
    setup() {

        // 获取全部数据
        const { getAlldata, tableData, total, params, changePage } = useGetData()
        // 编辑
        const { editFormVisible, editform, editFormRules, editRuleFormRef, handleredit, editSubmitForm, editResetForm } = useEditData()

        // 查看
        const {
            // dialogTableVisible,
            dialogFormVisible,
            formLabelWidth,
            checkInfo,
            checkInfoData, 
        } = useCheckInfo()
        // 查询
        const { queryForm, queryRuleFormRef, queryRule, onQuerySubmit } = useQuery()
        // 删除
        const { handleDel, handleSelectionChange, delBtn} = useDelete()
        // 新增
        const {addFormVisible, addInfo, addRuleFormRef, addResetForm, 
        closeDialog, addFormRules, addform, addSubmitForm} = useAddInfo()

        // 调用查询全部数据的方法
        getAlldata()

        return {
            queryForm,
            tableData,
            // 获取全部数据
            getAlldata,
            tableData,
            total,
            params,
            changePage,  //分页

            // 查看
            // dialogTableVisible,
            dialogFormVisible,
            formLabelWidth,
            checkInfo,
            checkInfoData,

            //  编辑对话框
            editform,
            editFormVisible, 
            editFormRules,
            editRuleFormRef,
            handleredit,
            editSubmitForm,
            editResetForm,

            // 查询
            queryForm,
            queryRuleFormRef,
            queryRule,
            onQuerySubmit,

            // 删除
            handleDel,
            delBtn,
            handleSelectionChange,
            InfoFilled,

            // 添加
            addFormVisible, 
            addInfo,
            addResetForm,
            closeDialog,
            addRuleFormRef,
            addFormRules,
            addform,
            addSubmitForm
        }
    },
}
</script>

<style lang="less" scoped>
#specialtyline {
    .title {
        font-size: 17px;
        color: #101010;
        text-align: left;
        letter-spacing: 1px;
        margin-bottom: 20px;
    }

    .main {
    // 标题
        .main_header {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .main_header_title {
                color: #009EFF;
                font-size: 16px;
                padding: 0 2px 5px 2px;
                border-bottom: 2px solid #009EFF;
            }
            .main_header_tool {
                font-size: 14px;

                .tool_btn {
                    width: 120px;
                    background: #1EB96B;
                    border-color: #1EB96B;
                }
            }
        }

    // 查询
        .main_query {
            display: flex;
            justify-content: space-between;
            margin: 40px 0;

            .main_query_form {
                flex-grow: 1;
            }

            .main_query_tool {
                width: 250px;
                display: flex;
                align-items: center;

                .tool_btn {
                    width: 80px;
                    background: #27B148;
                    border-color: #27B148;
                }
            }
        }

    // 数据显示
        .main_table {
            .tool_btn {
                height: 28px;
                width: 65px;
            }

            .check {
                background: #27B148;
                border-color: #27B148;
            }
        }

        .allDel {
            width: 100%;
            display: flex;
            justify-content: flex-start;
            margin-top: 20px;
        }
    }
}

/deep/.el-button {
    >span {
        font-size: 14px;
    }
}

/deep/.el-table td.el-table__cell, .el-table th.el-table__cell.is-leaf,
/deep/.el-table td.el-table__cell, .el-table th.el-table__cell.is-leaf {
    text-align: center;
}

/deep/.el-table th.el-table__cell.is-leaf{
    text-align: center;
    background: #E6F1FF;
}

/deep/.el-pagination.is-background .btn-next, 
/deep/.el-pagination.is-background .btn-prev, 
/deep/.el-pagination.is-background .el-pager .number{
    border: 1px solid #E8E8E8;
    background: #fff;
}

/deep/.el-pagination{
    justify-content: center;
}

/deep/.el-pagination.is-background .el-pager li:not(.is-disabled).is-active{
    background: #fff;
    color: #4C555D;
    font-weight: 500;
    border: 1px solid #1684FC;
}

/deep/.el-pager li.btn-quicknext, 
/deep/.el-pager li.btn-quickprev {
    border: 1px solid #E8E8E8;
    background: #fff;
}

/deep/.el-col-6 {
    text-align: left;
}

/deep/ .el-dialog {
//   width:70%;
  .el-dialog__body {
    display: flex;
    justify-content: flex-start;
    .el-form {
        flex:1;
        .el-form-item {
            &:nth-child(1),
            &:nth-child(2),
            &:nth-child(3),
            &:nth-child(4),
            &:nth-child(5),
            &:nth-child(6),
            &:nth-child(7),
            &:nth-child(8),
            &:nth-child(12),
            &:nth-child(13),
            &:nth-child(14),
            &:nth-child(15) {
                width: 430px;
            }
            &:nth-child(9) {
                width: 270px;
                margin-right: 5px;
            }
            &:nth-child(10),
            &:nth-child(11) {
                width: 130px;
                margin-right: 5px;
            }
            margin-right: 20px;
            .el-form-item__content  {
                width:300px;
                // padding-left: 8px;
                .el-input__inner {
                    text-align:center;
                }
            }
        }
    }
  }
}

.checkInfo {
  /deep/.el-form-item__label {
    color:#999;
    font-weight:600;
  }
}
</style>